package gocomm

import (
	"fmt"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

// Capitalize 字符首字母大写
func Capitalize(str string) string {
	var upperStr string

	vv := []rune(str) // 后文有介绍
	for i := 0; i < len(vv); i++ {
		if i == 0 {
			if vv[i] >= 97 && vv[i] <= 122 { // 后文有介绍
				vv[i] -= 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {

				return str
			}
		} else {

			upperStr += string(vv[i])
		}
	}
	return upperStr
}

// value: 0x1b -> 1b 不需要Ox
// base 表示 输入的数据是 哪一种进制
func HexStringToInt64(value string) (int64, error) {
	return strconv.ParseInt(value, 16, 64)
}
func StringToInt64(value string) int64 {

	i, _ := strconv.ParseInt(value, 10, 64)
	return i
}
func StringToInt32(value string) int32 {

	i, _ := strconv.ParseInt(value, 10, 32)
	return int32(i)
}
func Int64ToString(i int64) string {

	str := strconv.FormatInt(i, 10)
	return str
}

func Int32ToString(i int32) string {
	str := strconv.FormatInt(int64(i), 10)
	return str
}
func IntToString(i int) string {
	str := strconv.Itoa(i)
	return str
}
func Float64ToString(f float64) string {
	return strconv.FormatFloat(f, 'f', -1, 64)
}

func StringToFloat64(v string) float64 {
	f, _ := strconv.ParseFloat(v, 64)
	return f
}

/*
随机生成指定位数的数字
*/
func RandomNumber(numLen int) (code string) {
	rand.Seed(time.Now().UnixNano())

	code = ""
	for i := 0; i < numLen; i++ {
		n := rand.Intn(9)
		code = code + strconv.Itoa(n)
	}

	return
}

func StringToInt(value string) int {
	i, _ := strconv.Atoi(value)
	return i
}

func RemoveSlice(slice []interface{}, index int) []interface{} {
	ss := append(slice[:index], slice[index+1:])
	return ss
}

func InsertSlice(slice []interface{}, index int, o interface{}) {
	rear := append([]interface{}{}, slice[index:]...)

	slice = append(slice[0:index], o)
	slice = append(slice, rear)
}

func ResultIntParser(a int, err error) int {
	return a
}

// 保留小数位数
func FloatPrecision(p int, f float64) (float64, error) {
	str := "%." + strconv.Itoa(p) + "f"
	return strconv.ParseFloat(fmt.Sprintf(str, f), 64)
}

// 反转数组
func Reverse(data []string) *[]string {
	new_date := make([]string, 0)

	var cnt int
	cnt = len(data)
	for i := cnt - 1; i >= 0; i-- {
		new_date = append(new_date, data[i])
	}
	return &new_date
}

// RandomStr 随机生成字符串
func RandomStr(length int) string {
	str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	bytes := []byte(str)
	result := []byte{}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < length; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

// 获取指针中的值； value=nil 是不能 (*nil)
func GetStringPointer(value *string) string {
	if value == nil {
		return ""
	}
	return *value
}

// 把 []interface{} => []string
func ObjectArrayToStringArray(arr []interface{}) (*[]string, error) {

	if len(arr) > 0 {
		result := make([]string, 0)
		for _, item := range arr {
			result = append(result, item.(string))
		}
		return &result, nil
	}
	return nil, nil
}

func CreatActivationCode(l int) string {
	str := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	bytes := []byte(str)
	result := make([]byte, 0)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < l; i++ {
		result = append(result, bytes[r.Intn(len(bytes))])
	}
	return string(result)
}

// 判断生成的口令字符是否符合
func IsTokenChars(str string) bool {
	chars := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	size := len(str)
	i := 0
	for i < size {

		if !strings.Contains(chars, str[i:i+1]) {
			return false
		}
		i++
	}

	return true
}

// 去掉字符串后面特定字符
func TrimSuffix(s, suffix string) string {
	if strings.HasSuffix(s, suffix) {
		s = s[:len(s)-len(suffix)]
	}
	return s
}
